home *** CD-ROM | disk | FTP | other *** search
- /*
- Name: ntblockecho.rexx
-
- Version: 2.0
-
- Author: alfie
-
- Date: 28.04.98
-
- Description: shows how to make a non blocking TCP connection;
- the stopping-connetction event is a msg on
- port "PORT";
-
- Usage: rx ntblockecho <host>
-
- Note: it is a simple echo client in itself.
- Be sure to have echo/tcp enabled in the service
- and inetd database, before testing it on localhost.
- Anyway, 'cause of a sort of delay is required to
- test the stopping-event, try this macro on a very
- slow non-local host with echo/tcp service.
- */
-
- if ~show("L","rexxsupport.library") then
- if ~addlib("rexxsupport.library",0,-30) then do
- say "no rexxsupport.library"
- exit
- end
- if ~show("L","rxsocket.library") then
- if ~addlib("rxsocket.library",0,-30) then do
- say "no rxsocket.library"
- exit
- end
- if ~show("L","rmh.library") then
- if ~addlib("rmh.library",0,-30) then do
- say "no rmh.library"
- exit
- end
-
- prg = ProgramName("NOEXT")
-
- if ~RMH_ReadArgs("HOST/A") then do
- call PrintFault(IoErr(),prg)
- exit
- end
-
- addr = resolve(parm.0.value)
- if addr=="-1" then call err "no host <"parm.0.value">"
-
- /*set the remote addr*/
- sin.addrFamily = "INET"
- sin.addrAddr = addr
-
- /*try to find the echo service via GetServByName()*/
- if GetServByName("SE","echo","tcp") then sin.addrPort = SE.servPort
- else sin.addrPort = 7
-
- /*open a port, a message on which will stop all*/
- if ~OpenPort("PORT") then call err "can't open my port"
-
- /*get the port signal mask (not bit)*/
- portSig = PortSignal("PORT")
-
- /*allocate a signal we want to be notified on for a socket events*/
- sigBit=AllocSignal()
- if sigBit==-1 then call err "can't allocate signal bit"
- sig=2**sigBit
-
- /*create the socket*/
- sock = socket("INET","STREAM","IP")
- if sock<0 then call err "can't create socket ("errno()")"
-
- /*the socket must NOT block*/
- call IOCtlSocket(sock,"FIONBIO",1)
-
- /*notif us on connection*/
- call SetSockOpt(sock,"SOCKET","EVENTMASK","CONNECT")
-
- /*notify us via that signal we allocated*/
- SET.SIGEVENTMASK = sig
- call SetSocketBase("SET")
-
- /*connect the echo service on the host*/
- res = connect(sock,"SIN")
- err=errno()
- if (res<0) & (err~=35) & (err~=36) then call err "connect error ("errno()")"
-
- /*we even set a timeout of 10 seconds*/
- timeout = 10
- signal = or(portSig,sig)
- SEL.WRITE.0 = sock
- con = 0
- time = time("R")
- do while ~con & (time("E")<timeout)
-
- res = WaitSelect("SEL",timeout,0,signal)
-
- if and(SEL.SIGNALS,portSig)~=0 then do
- pkt = GetPkt("PORT")
- if pkt~=null() then do
- m = GetArg(pkt)
- call reply(pkt)
- say "message on PORT:" m
- end
- end
-
- if res==1 then con = 1;
-
- end
-
- if ~con then call err "timeout"
-
- say "--- connected ---"
-
- call IOCtlSocket(sock,"FIONBIO",0)
-
- request = "echo service test"
- res = send(sock,request)
- if res~=length(request) then call err "send errore ("errno()")"
-
- len = recv(sock,"BUFF",256)
- if len<0 then call err "recv error ("errno()")"
- say buff
- exit
-
- err: procedure expose prg
- parse arg msg
- say prg":" msg
- exit
-